﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;

namespace DiscoveryLogic.DataServices.DataSources
{
    public class BillPayeeForDataBinding : INotifyPropertyChanged 
    {
        #region Public Variables
        public const string PROPERTY_AMOUNT = "Amount";
        public const string PROPERTY_WITHDRAWL_DATE_STRING = "WithdrawlDateShortString";
        public const string PROPERTY_IS_RECURRING = "IsRecurring";

        #endregion

        #region Private Variables

        private string name;
        private string accNum;
        private bool isRecurring;
        private BillPaymentHistory billHistory;
        private double amount = 0;
        private DateTime withdrawlDate = DateTime.Now.Date;

        #endregion

        #region Constructor
        
        public BillPayeeForDataBinding(string PayeeName, string PayeeAccNum, bool Recurring, List<BillPayment> history)
        {
            name = PayeeName;
            accNum = PayeeAccNum;
            isRecurring = Recurring;
            billHistory = new BillPaymentHistory(history, PayeeName, 0);

            if (IsRecurring)
            {
                //set up next recurring payment
                Amount = GetLastAmount();
                DateTime lastWithdrawl = GetLastPaymentDate();
                DateTime nextWithdrawl = lastWithdrawl;
                while (nextWithdrawl < DateTime.Today)
                {
                    nextWithdrawl = nextWithdrawl.AddMonths(1);
                }
                WithdrawlDate = nextWithdrawl;
            }
        }

        #endregion

        #region Private Methods

        // Used to return date of last payment
        public DateTime GetLastPaymentDate()
        {
            // ASSUMPTION: last element in the history list is always the latest
            return billHistory.GetLastPaymentDate();
        }

        #endregion

        #region Get/Set methods

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string AccountNumber
        {
            get { return accNum; }
            set { accNum = value; }
        }

        public bool IsRecurring
        {
            get { return isRecurring; }
            set
            {
                isRecurring = value;
                NotifyPropertyChanged(PROPERTY_IS_RECURRING);
            }
        }

        public bool IsEditable
        {
            get { return !IsRecurring; }
        }
              
        public BillPaymentHistory BillPaymentHistory
        {
            get { return billHistory; }
            set { billHistory = value; }
        }

        public double Amount
        {
            get { return amount; }
            set
            {
                amount = value; 
                NotifyPropertyChanged(PROPERTY_AMOUNT);
            }
        }

        public DateTime WithdrawlDate
        {
            get { return withdrawlDate; }
            set
            {
                withdrawlDate = value.Date;
                NotifyPropertyChanged(PROPERTY_WITHDRAWL_DATE_STRING);
            }
        }

        public string WithdrawlDateShortString
        {
            get { return withdrawlDate.ToShortDateString(); }
        }

        #endregion

        #region Public Methods

        // Returns this Payee`s list of BillPayment objects
        public List<BillPayment> GetHistory()
        {
            // Moved history creation into BillPaymentManager. Should reefactor to a property.
            return billHistory.BillHistory;
        }

        // Used to return amount of last payment
        public double GetLastAmount()
        {
            // ASSUMPTION: last element in the history list is always the latest
            return billHistory.GetLastPaymentAmount();
        }

        #endregion

        #region Public INotifyPropertyChanged Events
        public event PropertyChangedEventHandler PropertyChanged;

        //Call event to notify UI of property change
        public void NotifyPropertyChanged(string propertyName)
        {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
        }
        #endregion
    }
}
